import {observable, useStrict, action, computed} from 'mobx';
 import data from '../testdata/order.json';
 


 
 export default class FoodState {
     // 数据源
     @observable list = [];
      @observable changedid = 0;
      @observable changeinid = 0;
      @observable showspecif=false;
      @observable leftindex=0;
 
  

    // 动态初始化数据
     @action getdata(data) {
         this.list=data
     }

    // 确定左边栏
     @action makeleftchose(index) {
         this.leftindex=index
     }
 
     // 加
     @action add() {
         this.list[1].list[1].listin[0].num++
         
     }
 
     // 减
     @action sub(id) {
         console.log('sub = '+ id);
         this.list[1].list[1].listin[0].num--
     };

     // 开启规格
     @action showspecif1(data) {
         this.showspecif=data
     };

     // 关闭规格
     @action close() {
         this.showspecif=false
     };
 
    // 判定重新渲染组件
     @action makesure(id1,id2) {
     
         this.changedid=parseInt(id1);
         this.changeinid=parseInt(id2)
     };

 
     // 计算总价
    @computed get totalPrice() {
         let totalprice = 0;
         let totalCount=0;
         let shoplist=[]
         this.list.forEach(function(da){
      da.list.forEach(function(item){
        item.listin.forEach(function(listin){
          if(listin.num>0){
            shoplist.push(listin)
            totalCount+=listin.num
            totalprice+=listin.price*listin.num
          }
        })
      })
    })
    return ({
      totalCount:totalCount,
      shopList:shoplist,
      totalPrice:parseFloat(totalprice).toFixed(2)
    })
     }

   //获得数据
   @computed get alldata(){
      
      this.list.forEach(function(da){
      da.list.forEach(function(item){
        item.listin.forEach(function(listin){
          if(listin.num>0){
            
          }
        })
      })
    })
     return this.list
   }

  //选中数据
   @computed get chosedfood(){
    let chosedfood=[]
    this.list.forEach(function(list){
      list.list.forEach(function(list){
        list.listin.forEach(function(listin){
          if(listin.num>0){
            
          chosedfood.push(listin)
        }
        })
      })
    })
    return chosedfood
  }
    
    
 } 